package com.pfzy.lib.sdk.driverlogger;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import androidx.core.app.NotificationManagerCompat;

import com.pfzy.lib.sdk.log.UXLog;
import com.pfzy.lib.sdk.utils.NetUtils;

import java.util.HashMap;

/**
 * 司机日志
 */
public class UXDriverLogger {
    public static final String TAG = "UXDriverLogger";
    private BroadcastReceiver mRecentlyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String reason = intent.getStringExtra("reason");
            if (reason != null) {
                HashMap<String, String> p = new HashMap<>();
                p.put("param1", "1");
                p.put("param2", reason);
                UXLog.i(UXDriverLogger.TAG, "recent:" + reason);
            }
        }
    };
    private BroadcastReceiver mScreenStatusReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            HashMap<String, String> p = new HashMap<>();
            p.put("param1", "2");
            if ("android.intent.action.SCREEN_ON".equals(action)) {
                p.put("param2", "android.intent.action.SCREEN_ON");
                UXLog.i(UXDriverLogger.TAG, "android.intent.action.SCREEN_ON");
            } else if ("android.intent.action.SCREEN_OFF".equals(action)) {
                p.put("param2", "android.intent.action.SCREEN_OFF");
                UXLog.i(UXDriverLogger.TAG, "android.intent.action.SCREEN_OFF");
            } else if ("android.intent.action.USER_PRESENT".equals(action)) {
                p.put("param2", "android.intent.action.USER_PRESENT");
                UXLog.i(UXDriverLogger.TAG, "android.intent.action.USER_PRESENT");
            }
        }
    };

    private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("android.intent.action.ACTION_POWER_DISCONNECTED")) {
                UXLog.i(UXDriverLogger.TAG, "battery disconnected..");
            } else if (action.equals("test.charge")) {
                UXLog.i(UXDriverLogger.TAG, "battery charging!");
            } else if (action.equals("test.uncharge")) {
                UXLog.i(UXDriverLogger.TAG, "battery disconnected..");
            } else if (action.equals("android.intent.action.ACTION_POWER_CONNECTED")) {
                UXLog.i(UXDriverLogger.TAG, "battery charging!");
            }
        }
    };

    private BroadcastReceiver mNetworkReceiver = new BroadcastReceiver() {
        private boolean mRegisterConnectivityAction = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null && intent.getAction() != null) {
                if (!this.mRegisterConnectivityAction) {
                    this.mRegisterConnectivityAction = true;
                    return;
                }
                boolean availabe = NetUtils.isNetworkAvailableForImmediately(context);
                UXLog.i(UXDriverLogger.TAG, "isNetworkAvailable:" + availabe);
                HashMap<String, String> p = new HashMap<>();
                p.put("param1", availabe + "");
            }
        }
    };

    public void init(Context context) {
        // 关闭系统对话框
        context.registerReceiver(this.mRecentlyReceiver, new IntentFilter("android.intent.action.CLOSE_SYSTEM_DIALOGS"));
        // 屏幕状态接收器
        IntentFilter screenStatusFilter = new IntentFilter();
        screenStatusFilter.addAction("android.intent.action.SCREEN_ON");
        screenStatusFilter.addAction("android.intent.action.SCREEN_OFF");
        screenStatusFilter.addAction("android.intent.action.USER_PRESENT");
        context.registerReceiver(this.mScreenStatusReceiver, screenStatusFilter);
        // 电池接收器
        IntentFilter batteryFilter = new IntentFilter();
        batteryFilter.addAction("android.intent.action.ACTION_POWER_CONNECTED");
        batteryFilter.addAction("android.intent.action.ACTION_POWER_DISCONNECTED");
        context.registerReceiver(this.mBatteryReceiver, batteryFilter);
        // 网络接收器
        IntentFilter networkReceiverFilter = new IntentFilter();
        networkReceiverFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        context.registerReceiver(this.mNetworkReceiver, networkReceiverFilter);
        checkNotificationOpen(context);
    }

    /**
     * 检查通知打开
     *
     * @param context
     */
    private void checkNotificationOpen(Context context) {
        try {
            boolean open = NotificationManagerCompat.from(context).areNotificationsEnabled();
            HashMap<String, String> p = new HashMap<>();
            p.put("param1", open + "");
            UXLog.i(TAG, "notification open:" + open);
        } catch (Exception e) {
            UXLog.e(TAG, e.toString());
        }
    }

    public void onDestroy(Context context) {
        context.unregisterReceiver(this.mRecentlyReceiver);
        context.unregisterReceiver(this.mScreenStatusReceiver);
        context.unregisterReceiver(this.mBatteryReceiver);
        context.unregisterReceiver(this.mNetworkReceiver);
    }
}
